package com.sjh;

import java.util.concurrent.*;

/**
 * @author jack
 * @data 2020/8/24 5:10 下午
 */
public class ThreadPoolTest {
    public static void main(String[] args) {
//        创建固定大小的线程池
        ExecutorService threadPool = Executors.newFixedThreadPool(3);
//        创建缓存线程池
//        ExecutorService threadPool = Executors.newCachedThreadPool();
//        创建单一线程池(可实现线程死掉后重新启动)
//        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        for (int j = 0; j < 10; j++) {
            final int task = j;
            threadPool.execute(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + " is pool of " + i + " for task " + task);
                    }
                }
            });
        }
        threadPool.shutdown();
        Executors.newScheduledThreadPool(3).schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("bombing");
            }
        },3, TimeUnit.SECONDS);


    }
}
